//
//  BBNodeValuesView.m
//  xNose
//
//  Created by ben smith on 9/10/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "BBNodeValuesView.h"
#import "BBNode.h"
#import "BBDefaultController.h"


@implementation BBNodeValuesView

@synthesize isEditable;

- (id)initWithFrame:(NSRect)frame {
	self = [super initWithFrame:frame];
	if (self) {
		// set the text containers
		float fontHeight = [BBDefaultController fontHeight:[self font]];
		// start at the Bottom
		
		NSRect leftRect = NSMakeRect(0, 0, NSWidth(self.bounds)/2.0, fontHeight);
		NSRect rightRect = NSMakeRect(NSWidth(self.bounds)/2.0, 0, NSWidth(self.bounds)/2.0, fontHeight);

		NSString * localString = [[NSUserDefaults standardUserDefaults] objectForKey:@"BBProfitString"];	

		NSTextField * left = [self fieldWithFrame:leftRect];
		[left setStringValue:[NSString stringWithFormat:@"%@:",localString]];
		[self addSubview:left];
		[left release];
		
		profit = [self fieldWithFrame:rightRect];
		[profit setFormatter:[BBDefaultController percentFormatter]];
		[profit setAlignment:NSRightTextAlignment];
		[profit setEditable:NO];
		[self addSubview:profit];
		
		leftRect.origin.y += fontHeight;
		rightRect.origin.y += fontHeight;

		localString = [[NSUserDefaults standardUserDefaults] objectForKey:@"BBIncomeString"];	

		left = [self fieldWithFrame:leftRect];
		[left setStringValue:[NSString stringWithFormat:@"%@:",localString]];
		[self addSubview:left];
		[left release];
		
		income = [self fieldWithFrame:rightRect];
		[income setAlignment:NSRightTextAlignment];
		[income setFormatter:[BBDefaultController currencyFormatter]];
		[self addSubview:income];

		
		leftRect.origin.y += fontHeight;
		rightRect.origin.y += fontHeight;
		
		localString = [[NSUserDefaults standardUserDefaults] objectForKey:@"BBCostString"];	

		left = [self fieldWithFrame:leftRect];
		[left setStringValue:[NSString stringWithFormat:@"%@:",localString]];
		[self addSubview:left];
		[left release];
		
		cost = [self fieldWithFrame:rightRect];
		[cost setAlignment:NSRightTextAlignment];
		[cost setFormatter:[BBDefaultController currencyFormatter]];
		[self addSubview:cost];
		
		
		leftRect.origin.y += fontHeight;
		rightRect.origin.y += fontHeight;
		
		leftRect.size.width += rightRect.size.width;

		boldTitle = [self fieldWithFrame:leftRect];
		[boldTitle setStringValue:@"Module"];
		[self addSubview:boldTitle];
	}
	return self;
}


-(void)setNode:(BBNode*)aNode
{
	[self.node removeObserver:self forKeyPath:@"income"];
	[self.node removeObserver:self forKeyPath:@"cost"];
	
	[aNode retain];
	[node release];
	node = aNode;

	[self.node addObserver:self forKeyPath:@"income" options:NSKeyValueObservingOptionNew context:nil];
	[self.node addObserver:self forKeyPath:@"cost" options:NSKeyValueObservingOptionNew context:nil];
	if (node != nil)	[boldTitle setStringValue:node.title];
	[self updateFields];
}

-(void)drawNode
{
	[[NSColor whiteColor] set];
	NSRect frame = [boldTitle frame];
	[NSBezierPath strokeLineFromPoint:NSMakePoint(NSMinX(frame), NSMinY(frame) + 2) toPoint:NSMakePoint(NSMaxX(frame), NSMinY(frame) + 2)];
	//[[NSColor redColor] set];
	//[NSBezierPath strokeRect:[self bounds]];
}

- (void)setupTextAttributes
{
	
}

- (void)fieldDone:(id)sender
{
	if (sender == cost) self.node.cost = [cost doubleValue];
	if (sender == income)	self.node.income = [income doubleValue];
	[[NSNotificationCenter defaultCenter] postNotificationName:@"BBReleaseEditNotification" object:nil];	
}

- (void)controlTextDidEndEditing:(NSNotification *)aNotification
{
	if ([aNotification object] == cost) self.node.cost = [cost doubleValue];
	if ([aNotification object] == income)	self.node.income = [income doubleValue];
}	

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
	[self updateFields];
}

-(void)updateFields
{
	if (self.node == nil) return;
	
	float roundTo = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBRadialRoundValue"];
	
	int costRounded = (self.node.cost / roundTo);
	costRounded *= roundTo;
	[cost setIntValue:costRounded];
	
	int incRounded = (self.node.income / roundTo);
	incRounded *= roundTo;
	
	[income setIntValue:incRounded];
	
	[profit setDoubleValue:self.node.profit];
	
	[income setEditable:isEditable];
	[income setSelectable:isEditable];
	[cost setEditable:isEditable];
	[cost setSelectable:isEditable];
	
	[self setNeedsDisplay:YES];
}


-(NSTextField*)fieldWithFrame:(NSRect)frame
{
	NSTextField * field = [[NSTextField alloc] initWithFrame:frame];
	[field setAlignment:NSLeftTextAlignment];
	[field setFont:[self font]];
	[field setDrawsBackground:NO];
	[field setBordered:NO];
	[field setEditable:NO];
	[field setSelectable:NO];
	[field setDelegate:self];
	return field;
}


-(NSFont*)font
{
	return [BBDefaultController regularSmallFont];
}


- (void) dealloc
{	
	[self.node removeObserver:self forKeyPath:@"income"];
	[self.node removeObserver:self forKeyPath:@"cost"];
	[cost release];
	[income release];
	[profit release];
	[super dealloc];
}


@end
